<!--流程设计器：包含基础设计器和属性面板等控件-->
<template>
  <div class="bpm-out-box-body">
    <div class="bpm-box-div">
      <!-- <my-process-palette /> -->
      <my-process-designer
        :key="`designer-${reloadIndex}`"
        :options="{
          taskResizingEnabled: true,
          eventResizingEnabled: true,
          minimap: {
            open: false
          }
        }"
        v-bind="controlForm"
        v-model="xmlStringBpm"
        keyboard
        ref="processDesigner"
        @element-click="elementClick"
        @element-contextmenu="elementContextmenu"
        @init-finished="initFinished"
        @change="changedCallback"
      />
      <my-properties-panel :key="`penal-${reloadIndex}`" :bpmn-modeler="modeler" :prefix="controlForm.prefix" class="process-panel" />

      <!-- demo config 
      <div class="demo-control-bar">
        <div class="open-model-button" @click="controlDrawerVisible = true"><el-icon><setting /></el-icon></div>
      </div>-->
      <!--
      <el-drawer v-model="controlDrawerVisible" size="400px" title="偏好设置" append-to-body destroy-on-close>
        <el-form :model="controlForm" size="small" label-width="100px" class="control-form" @submit.prevent>
          <el-form-item label="流程ID">
            <el-input v-model="controlForm.processId" @change="reloadProcessDesigner(true)" />
          </el-form-item>
          <el-form-item label="流程名称">
            <el-input v-model="controlForm.processName" @change="reloadProcessDesigner(true)" />
          </el-form-item>
          <el-form-item label="流转模拟">
            <el-switch v-model="controlForm.simulation" inactive-text="停用" active-text="启用" @change="reloadProcessDesigner()" />
          </el-form-item>
          <el-form-item label="禁用双击">
            <el-switch v-model="controlForm.labelEditing" inactive-text="停用" active-text="启用" @change="changeLabelEditingStatus" />
          </el-form-item>
          <el-form-item label="自定义渲染">
            <el-switch v-model="controlForm.labelVisible" inactive-text="停用" active-text="启用" @change="changeLabelVisibleStatus" />
          </el-form-item>
          <el-form-item label="流程引擎">
            <el-radio-group v-model="controlForm.prefix" @change="reloadProcessDesigner()">
              <el-radio label="camunda">camunda</el-radio>
              <el-radio label="flowable">flowable</el-radio>
              <el-radio label="activiti">activiti</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="工具栏">
            <el-radio-group v-model="controlForm.headerButtonSize">
              <el-radio label="small">small</el-radio>
              <el-radio label="default">default</el-radio>
              <el-radio label="large">large</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-switch v-model="pageMode" active-text="dark" inactive-text="light" @change="changePageMode"></el-switch>
        </el-form>
      </el-drawer>-->

      <!-- <div class="demo-info-bar">
        <div class="open-model-button" @click="infoTipVisible = !infoTipVisible"><el-icon><InfoFilled /></el-icon></div>
          <transition name="zoom-in-right">
            <div class="info-tip" v-show="infoTipVisible">
              <p><strong>该项目仅作为Bpmn.js的简单演示项目，不涉及过多的自定义Render内容。</strong></p>
              <p>注：activiti 好像不支持表单配置，控制台可能会报错</p>
              <p>
                <span>更多配置请查看源码：</span>
                <a href="https://github.com/miyuesc/bpmn-process-designer">MiyueSC/bpmn-process-designer</a>
              </p>
              <p>
                <span>bpmn 官方图标：</span>
                <a href="https://cdn.staticaly.com/gh/bpmn-io/bpmn-font/master/dist/demo.html">bpmn-io/bpmn-font</a>
              </p>
              <p>
                <span>疑问请在此留言：</span>
                <a href="https://github.com/miyuesc/bpmn-process-designer/issues/16">MiyueSC/bpmn-process-designer/issues</a>
              </p>
            </div>
          </transition>
        </div> 
      </div>-->
    </div>
  </div>
</template>

<script setup>
import translations from '@/components/bpmnProcess/translations';
// 自定义渲染（隐藏了 label 标签）
import CustomRenderer from '@/components/custom-renderer';
// 自定义元素选中时的弹出菜单（修改 默认任务 为 用户任务）
import CustomContentPadProvider from '@/components/bpmnProcess/designer/plugins/content-pad';
// 自定义左侧菜单（修改 默认任务 为 用户任务）
import CustomPaletteProvider from '@/components/bpmnProcess/designer/plugins/palette';
import Log from '@/components/bpmnProcess/Log';
// 任务resize
// import resizeTask from "bpmn-js-task-resize/lib";
// 小地图 引入未生效，暂时删除
// import minimapModule from 'diagram-js-minimap';

import 'bpmn-js/dist/assets/diagram-js.css';
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css';

const props = defineProps({
  xmlString: {
    // 流程设计xml内容 BPMN XML 字符串 对应指令v-model:xml-string
    type: String,
    default: "",
  },
  processId: {
    // 流程id，也就是流程key（模型名称modelKey）
    type: String,
    default: "",
  },
  processName: {
    // 流程名称，也就是name（模型名称modelName）
    type: String,
    default: "",
  },
  prefix: {
    // 使用哪个引擎:activiti、camunda、flowable
    type: String,
    default: "activiti",
  },
});

const emit = defineEmits(["update:xmlString","change"]);

const modeler = ref(null)
const reloadIndex = ref(0)
const element =ref(null)
const controlDrawerVisible = ref(false)
const infoTipVisible = ref(false)
const pageMode = ref(false)
const translationsSelf = ref(translations)
const controlForm = reactive({
  processId: '',
  processName: '',
  simulation: true,
  labelEditing: false,
  labelVisible: false,
  prefix: 'activiti',
  headerButtonSize: 'default',
  events: ['element.click', 'element.contextmenu'],
  additionalModel: [CustomContentPadProvider]// CustomPaletteProvider minimapModule
})
const addis = reactive({
  CustomContentPadProvider
  //,CustomPaletteProvider
})
const xmlStringBpm = ref(null)

//此时可以得到传入的参数值
controlForm.prefix = props.prefix;
controlForm.modelValue = props.xmlString;
controlForm.processId = props.processId;
controlForm.processName = props.processName;
xmlStringBpm.value = props.xmlString;
//console.log('BpmProcessDesignner xmlString:'+ props.xmlString);

function initFinished(modelerVal) {
  setTimeout(() => {
    modeler.value = modelerVal;
    const canvas = modeler.value.get('canvas');
    const rootElement = canvas.getRootElement();
    Log.prettyPrimary('Process Id:', rootElement.id);
    Log.prettyPrimary('Process Name:', rootElement.businessObject.name);
  }, 10);
}

function changedCallback(val){
  emit("update:xmlString", val);
  emit("change",val);
}

function reloadProcessDesigner(notDeep) {
  controlForm.additionalModel = [];
  for (const key in addis) {
    if (addis[key]) {
      controlForm.additionalModel.push(addis[key]);
    }
  }
  !notDeep;
  reloadIndex.value = reloadIndex.value + 1;
  modeler.value = null; // 避免 panel 异常
}

function changeLabelEditingStatus(status) {
  addis.labelEditing = status ? { labelEditingProvider: ['value', ''] } : false;
  reloadProcessDesigner();
}

function changeLabelVisibleStatus(status) {
  addis.customRenderer = status ? CustomRenderer : false;
  reloadProcessDesigner();
}

function elementClick(elementVal) {
  //console.log(element);
  element.value = elementVal;
}

function elementContextmenu(element) {
  console.log('elementContextmenu:', element);
}

function changePageMode(mode) {
  const theme = mode
    ? {
      // dark
      stroke: '#ffffff',
      fill: '#333333'
    }
    : {
      // light
      stroke: '#000000',
      fill: '#ffffff'
    };
  const elements = modeler.value.get('elementRegistry').getAll();
  modeler.value.get('modeling').setColor(elements, theme);
}

</script>

<style lang="scss">
.bpm-window-el-dialog > .el-dialog__body{
  height: 86vh;
  overflow: auto;
  padding: 0;
}
.bpm-out-box-body {
  overflow: hidden;
  margin: 0;
  box-sizing: border-box;
  /*height: 700px;*/
}
.bpm-box-div{
  width: 100%;
  box-sizing: border-box;
  display: inline-grid;
  grid-template-columns: 100px auto max-content;
  position: relative;
  border: 1px solid #e5e6e7;
}
.process-panel{
  position: absolute;
  right: 0px;
  top: 48px;
  border: 1px solid #e5e6e7;
  padding: 0px 8px;
  background-color: #fff;
}
/* 格子背景 */
.my-process-designer .my-process-designer__container .my-process-designer__canvas {
    background: url() repeat !important;
}

.demo-info-bar {
  position: fixed;
  right: 8px;
  bottom: 108px;
  z-index: 1;
}
.demo-control-bar {
  position: fixed;
  right: 8px;
  bottom: 48px;
  z-index: 1;
}
.open-model-button {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  font-size: 32px;
  background: rgba(64, 158, 255, 1);
  color: #ffffff;
  cursor: pointer;
}
.zoom-in-right-enter-active,
.zoom-in-right-leave-active {
  opacity: 1;
  transform: scaleY(1) translateY(-48px);
  transition: all 300ms cubic-bezier(0.23, 1, 0.32, 1);
  transform-origin: right center;
}
.zoom-in-right-enter,
.zoom-in-right-leave-active {
  opacity: 0;
  transform: scaleX(0) translateY(-48px);
}
.info-tip {
  position: absolute;
  width: 480px;
  top: 0;
  right: 64px;
  z-index: 10;
  box-sizing: border-box;
  padding: 0 16px;
  color: #333333;
  background: #f2f6fc;
  transform: translateY(-48px);
  border: 1px solid #ebeef5;
  border-radius: 4px;
  &::before,
  &::after {
    content: "";
    width: 0;
    height: 0;
    border-width: 8px;
    border-style: solid;
    position: absolute;
    right: -15px;
    top: 50%;
  }
  &::before {
    border-color: transparent transparent transparent #f2f6fc;
    z-index: 10;
  }
  &::after {
    right: -16px;
    border-color: transparent transparent transparent #ebeef5;
    z-index: 1;
  }
}
.control-form {
  .el-radio {
    width: 100%;
    line-height: 32px;
  }
}
.element-overlays {
  box-sizing: border-box;
  padding: 8px;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 4px;
  color: #fafafa;
}

.bpm-out-box-body,
.bpm-out-box-body * {
  /* 滚动条 */
  &::-webkit-scrollbar-track-piece {
    background-color: #fff; /*滚动条的背景颜色*/
    -webkit-border-radius: 0; /*滚动条的圆角宽度*/
  }
  &::-webkit-scrollbar {
    width: 10px; /*滚动条的宽度*/
    height: 8px; /*滚动条的高度*/
  }
  &::-webkit-scrollbar-thumb:vertical {
    /*垂直滚动条的样式*/
    height: 50px;
    background-color: rgba(153, 153, 153, 0.5);
    -webkit-border-radius: 4px;
    outline: 2px solid #fff;
    outline-offset: -2px;
    border: 2px solid #fff;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.3);
    -webkit-border-radius: 4px;
  }
  &::-webkit-scrollbar-thumb:hover {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.5);
    -webkit-border-radius: 4px;
  }
}

/* 按钮和标题距离过近问题 */
.panel-tab__content--title .el-button{
margin: 0 0 4px 20px;
}
.bjs-powered-by{
  display: none;
}
/* 将左侧两列式工具栏修改为一列：94px修改为48px */
.djs-palette-two-column .djs-palette.two-column.open {
    width: 48px;
}
</style>
